﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBO.Data.Rnd;

namespace PBO.Data
{
    public enum BattleType : byte
    {
        None,
        Normal,
        Fighting,
        Flying,
        Poison,
        Ground,
        Rock,
        Bug,
        Ghost,
        Steel,
        Fire,
        Water,
        Grass,
        Electric,
        Psychic,
        Ice,
        Dragon,
        Dark
    }

    public static class BattleTypeHelper
    {
        private static string[] NEW_NAMES = { "无", "一般", "格斗", "飞行", "毒", "地上", "岩石", "虫", "幽灵", "钢", "炎", "水", "草", "电", "超能力", "冰", "龙", "恶" };

        public static string ToCnName(this BattleType pokeType)
        {
            return NEW_NAMES[(int)pokeType];
        }

        #region EffectRevise

        private static readonly double[,] REVISE = 
        {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,0.5,1,0,0.5,1,1,1,1,1,1,1,1},
        {1,2,1,0.5,0.5,1,2,0.5,0,2,1,1,1,1,0.5,2,1,2},
        {1,1,2,1,1,1,0.5,2,1,0.5,1,1,2,0.5,1,1,1,1},
        {1,1,1,1,0.5,0.5,0.5,1,0.5,0,1,1,2,1,1,1,1,1},
        {1,1,1,0,2,1,2,0.5,1,2,2,1,0.5,2,1,1,1,1},
        {1,1,0.5,2,1,0.5,1,2,1,0.5,2,1,1,1,1,2,1,1},
        {1,1,0.5,0.5,0.5,1,1,1,0.5,0.5,0.5,1,2,1,2,1,1,2},
        {1,0,1,1,1,1,1,1,2,0.5,1,1,1,1,2,1,1,0.5},
        {1,1,1,1,1,1,2,1,1,0.5,0.5,0.5,1,0.5,1,2,1,1},
        {1,1,1,1,1,1,0.5,2,1,2,0.5,0.5,2,1,1,2,0.5,1},
        {1,1,1,1,1,2,2,1,1,1,2,0.5,0.5,1,1,1,0.5,1},
        {1,1,1,0.5,0.5,2,2,0.5,1,0.5,0.5,2,0.5,1,1,1,0.5,1},
        {1,1,1,2,1,0,1,1,1,1,1,2,0.5,0.5,1,1,0.5,1},
        {1,1,2,1,2,1,1,1,1,0.5,1,1,1,1,0.5,1,1,0},
        {1,1,1,2,1,2,1,1,1,0.5,0.5,0.5,2,1,1,0.5,2,1},
        {1,1,1,1,1,1,1,1,1,0.5,1,1,1,1,1,1,2,1},
        {1,1,0.5,1,1,1,1,1,2,0.5,1,1,1,1,2,1,1,0.5}};

        private static readonly int[,] REVISE2 =  { 
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,2,2,2,2,2,1,2,0,1,2,2,2,2,2,2,2,2},
{2,4,2,1,1,2,4,1,0,4,2,2,2,2,1,4,2,4},
{2,2,4,2,2,2,1,4,2,1,2,2,4,1,2,2,2,2},
{2,2,2,2,1,1,1,2,1,0,2,2,4,2,2,2,2,2},
{2,2,2,0,4,2,4,1,2,4,4,2,1,4,2,2,2,2},
{2,2,1,4,2,1,2,4,2,1,4,2,2,2,2,4,2,2},
{2,2,1,1,1,2,2,2,1,1,1,2,4,2,4,2,2,4},
{2,0,2,2,2,2,2,2,4,1,2,2,2,2,4,2,2,1},
{2,2,2,2,2,2,4,2,2,1,1,1,2,1,2,4,2,2},
{2,2,2,2,2,2,1,4,2,4,1,1,4,2,2,4,1,2},
{2,2,2,2,2,4,4,2,2,2,4,1,1,2,2,2,1,2},
{2,2,2,1,1,4,4,1,2,1,1,4,1,2,2,2,1,2},
{2,2,2,4,2,0,2,2,2,2,2,4,1,1,2,2,1,2},
{2,2,4,2,4,2,2,2,2,1,2,2,2,2,1,2,2,0},
{2,2,2,4,2,4,2,2,2,1,1,1,4,2,2,1,4,2},
{2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,4,2},
{2,2,1,2,2,2,2,2,4,1,2,2,2,2,4,2,2,1}};

        public static double EffectRevise(this BattleType atkType, BattleType defType)
        {
            double eff = REVISE[(int)atkType, (int)defType];
            return eff;
        }

        public static double EffectRevise(this BattleType a, params BattleType[] types)
        {
            double typeEff = 1;
            foreach (var type in types)
            {
                if (type != BattleType.None)
                {
                    typeEff *= a.EffectRevise(type);
                }
                if (typeEff == 0) return typeEff;
            }
            return typeEff;
        }

        public static double WeatherEffects(this BattleType pokeType, Weather weather)
        {
            if ((pokeType == BattleType.Water && weather == Weather.Rainy) ||
                (pokeType == BattleType.Fire && weather == Weather.Sunny))
            {
                return 1.5;
            }

            if ((pokeType == BattleType.Water && weather == Weather.Sunny) ||
                (pokeType == BattleType.Fire && weather == Weather.Rainy))
            {
                return 0.5;
            }
            return 1;
        }

        #endregion

        #region 属性切换2

        /// <summary>
        /// 变性2
        /// </summary>
        public static BattleType Conversion2Type(this BattleType moveType, Random rnd)
        {
            var arr = from BattleType t in Enum.GetValues(typeof(BattleType))
                      where moveType.EffectRevise(t) < 1
                      select t;
            return arr.ToArray().GetRandom(rnd);
        }

        #endregion

    }
}